package mall

import (
	"e_mall_server/models"
	"e_mall_server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

/**
 * @Author: zhaobin
 * @Description TODO
 * @Date: 2022-05-13 19:55
 */

// GetGoodsListByQuery 获取商品列表接口
func GetGoodsListByQuery(c *gin.Context) {
	goodsQueryParam := models.GoodsQueryParam{
		// 设置默认值
		//PageNum:  1,
		//PageSize: 20,
	}
	if err := c.ShouldBind(&goodsQueryParam); err != nil {
		zap.L().Error("GetGoodsList with invalid param", zap.Error(err))
		ResponseError(c, "请求参数错误")
		return
	}
	goodsResponse, err := service.GetGoodsListByQuery(&goodsQueryParam)
	if err != nil {
		zap.L().Error("service.GetGoodsList() failed", zap.Error(err))
		ResponseError(c, "查询商品列表失败")
		return
	}
	ResponseSuccess(c, goodsResponse)
}

// GetGoodsDetailById 通过id查询商品信息
func GetGoodsDetailById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		zap.L().Error("GetGoodsDetailById with invalid param", zap.Error(err))
		ResponseError(c, "请求参数错误")
	}
	goods, err := service.GetGoodsDetailById(id)
	if err != nil {
		zap.L().Error("service.GetGoodsDetailById(id) failed", zap.Error(err))
		ResponseError(c, "获取管理员信息错误")
		return
	}
	ResponseSuccess(c, goods)
}
